<?php
/**
 * Created by JetBrains PhpStorm.
 * User: 蒋文书
 * Date: 14-12-27
 * Time: 下午8:50
 * To change this template use File | Settings | File Templates.
 */

namespace Admin\Controller;
use Think\Controller;
use Think\Upload;

class ChannelController extends CommonController {

    /**
     * 栏目列表
     * @author jiangwenshu <jiangwenshu1990@126.com>
     */
    public function index(){
        $this->modelList=M('model')->order('sort desc')->select();
        $this -> meta_title = '栏目列表';
        $this -> display();
    }

    /**
     * 新增模型
     * @author jiangwenshu <jiangwenshu1990@126.com>
     */
    public function add(){
        //模型ID
        $modelId=I('get.modelId',0,'intval');
        //是否有内容
        $isChannel=I('get.isChannel',0,'intval');
        //模型ID不能为空

        $this -> fields =D('channel')->get_model_id($modelId,$isChannel);
        $this -> assign('modelId',$modelId)->assign('isChannel',$isChannel);
        if(IS_POST){
            if(D('Channel')->add()){
                $this->success('新增成功！',U('Index'));
            }else{
                $this->error('新增失败！');
            }
        }
        $this -> meta_title = '新增栏目';
        $this -> display(); 
    }

    /**
     * 编辑模型
     * @author jiangwenshu <jiangwenshu1990@126.com>
     */
    public function edit(){
        $id=I('id',0,'intval');
        if(IS_POST){
            if(D('model')->update()){
                $this->success('更新成功！',U('Index'));
            }else{
                $this->error('更新失败！');
            }
        }else{
            $this->info=D('model')->info($id);
        }

        $this -> meta_title = '编辑模型';
        $this -> display(); 
    } 

    /**
     * 删除模型
     * @author jiangwenshu <jiangwenshu1990@126.com>
     */
    public function del(){
        $id=I('get.id',0,'intval');
        if(D('model')->del($id)){
            $this->success('删除成功！',U('Index'));
        }else{
            $this->error('删除失败！');
        }
    }

    /**
     * 字段模型排序
     * @author jiangwenshu <jiangwenshu1990@126.com>
     */
     public function sort(){
        if(IS_POST){
            foreach ($_POST['sort'] as $id => $sort) {
                M('model_field')->where(array('field_id'=>$id))->save(array('sort'=>$sort));
            }
            $this->success("排序更新成功！");
        }else{
            $this->error('排序更新失败！');
        }
     }

    /**
     * 是否禁用 
     * @return status 开启 1：开启  0：禁用
     * @author jiangwenshu <jiangwenshu1990@126.com>
     */
     public function status(){
        $fieldid = I('get.fieldid', 0, 'intval');
        $status = (int) $_GET['status'] ? 0 : 1;
        if (D('model')->status($fieldid,$status)) {
            $this->success("操作成功！");
        } else {
            $this->error("操作失败！");
        }
     }


    /**
     * ueditor
     * @return status 开启 1：开启  0：禁用
     * @author jiangwenshu <jiangwenshu1990@126.com>
     */
    public function editor(){
        $setting = C('EDITOR_UPLOAD');
        $this -> uploader = new Upload($setting, 'LOCAL');
        $img   = $this->uploader->upload();
        $return = array();
        $return['url'] = $img['upfile']['savepath'].$img['upfile']['savename'];
        $title = htmlspecialchars($_POST['pictitle'], ENT_QUOTES);
        $return['title'] = $title;
        $return['original'] = $img['upfile']['name'];
        $return['state'] = ($img)? 'SUCCESS' : $this->uploader->getError();
        $this->ajaxReturn($return);
    }
}